<?php

namespace app\admin\controller;

use think\Db;

class Shop extends Common
{
    // 礼物列表
    public function gift()
    {
        $where = ['channel'=>1];

        $search_name = input('param.name');
        $search_type = input('param.type');
        if ($search_name && $search_type) {
            $where[$search_type] = ['like', '%' . $search_name . '%'];
        }

        $types = input('types');
        if ($types !== null) {
            $where['types'] = $types;
        }

        $channel_sex = input('channel_sex/d', 0);
        $where['channel_sex'] = $channel_sex;


        $data = Db::name('gift')->where($where)->order('id desc')->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        $this->assign('types', $types);
        $this->assign('channel_sex', $channel_sex);

        return $this->fetch();
    }

    /**
     * 添加礼物
     * @return string|string[]
     */
    public function addGift()
    {
        $id = input('id');

        if (request()->isAjax()) {

            $post = input('post.');
            $post['channel'] = 1;
            if (!$id) {
                $db = Db::name('gift')->insert($post);
                if ($db) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else {
                $db = Db::name('gift')->where('id', $id)->update($post);
                if ($db !== false) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }
        } else {
            if ($id) {
                $name = Db::name('gift')->find($id);
                if ($name) {
                    $this->assign('name', $name);
                } else {
                    return ['err' => 'id参数错误'];
                }
            }

            $data = $this->fetch();
        }
        return $data;
    }

    /**
     * 删除礼物
     * @return string[]
     */
    public function delGift()
    {
        if (request()->isAjax()) {

            $id = input('id/d');

            if ($id) {
                $result = Db::name('gift')->delete($id);

                $data = $result ? ['ret' => '删除成功'] : ['err' => '删除失败'];
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }

    /**
     * 座驾列表
     * @return string
     */
    public function car()
    {
        $where = [];

        $search_name = input('param.name');
        $search_type = input('param.type');
        if ($search_name && $search_type) {
            $where[$search_type] = ['like', '%' . $search_name . '%'];
        }

        $types = input('types');
        if ($types) {
            $where['types'] = $types;
        }

        $data = Db::name('car')->where($where)->order('id desc')->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        $this->assign('types', $types);

        return $this->fetch();
    }

    /**
     * 添加座驾
     * @return string|string[]
     */
    public function addCar()
    {
        $id = input('id');

        if (request()->isAjax()) {

            $post = input('post.');

            if (!$id) {
                $db = Db::name('car')->insert($post);
                if ($db) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else {
                $db = Db::name('car')->where('id', $id)->update($post);
                if ($db !== false) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }
        } else {
            if ($id) {
                $name = Db::name('car')->find($id);
                if ($name) {
                    $this->assign('name', $name);
                } else {
                    return ['err' => 'id参数错误'];
                }
            }

            $data = $this->fetch();
        }
        return $data;
    }

    /**
     * 删除座驾
     * @return string[]
     */
    public function delCar()
    {
        if (request()->isAjax()) {

            $id = input('id/d');

            if ($id) {
                $result = Db::name('car')->delete($id);

                $data = $result ? ['ret' => '删除成功'] : ['err' => '删除失败'];
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }
}
